package com.ruoyi.web.controller.agoniapp;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.UserIdByRequest;
import com.ruoyi.system.pojo.dto.AcceptTransactionDTO;
import com.ruoyi.system.pojo.dto.BuyerCreateTransactionDTO;
import com.ruoyi.system.pojo.vo.TransactionRecordUserVO;
import com.ruoyi.system.service.IAgoniTransactionRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/agoniapp/transactionRecord")
public class TransactionRecordController {
    @Autowired
    private IAgoniTransactionRecordService transactionRecordService;

    @PostMapping("/buyerCreateTransaction")
    public AjaxResult buyerCreateTransaction(@RequestBody BuyerCreateTransactionDTO buyerCreateTransactionDTO, HttpServletRequest request) {
        log.info("buyerCreateTransaction");
        if (buyerCreateTransactionDTO.getProductStatus() == 0){
            return AjaxResult.error("商品状态异常，请联系客服");
        }
        Long  buyerId = UserIdByRequest.getUserId(request);
        int result = transactionRecordService.buyerCreateTransaction(buyerCreateTransactionDTO,buyerId);
        if(result==0){
            return AjaxResult.error("创建交易记录失败，请联系客服");
        }
        return AjaxResult.success("创建交易记录成功");
    }

        @GetMapping("getTransactionRecordByToken")
    public AjaxResult getTransactionRecordByToken(HttpServletRequest request) {
        long userId = UserIdByRequest.getUserId(request);

            List<TransactionRecordUserVO> transactionRecordUserVOList = transactionRecordService.getTransactionRecordByToken(userId);



        return AjaxResult.success("获取成功",transactionRecordUserVOList);
        }

        @GetMapping("rejectTransaction/{id}")
    public AjaxResult rejectTransaction(@PathVariable("id") Long id) {

        int result = transactionRecordService.rejectTransaction(id);
        if(result==0){
            return AjaxResult.error("拒绝交易失败，请联系客服");
        }

        return AjaxResult.success("已拒绝交易");
    }


    //获取我的求购记录
    @GetMapping("getMyBuyRecordByToken")
    public AjaxResult getMyBuyRecordByToken(HttpServletRequest request) {
        long userId = UserIdByRequest.getUserId(request);

        List<TransactionRecordUserVO> transactionRecordUserVOList = transactionRecordService.getMyBuyRecordByToken(userId);

        return AjaxResult.success("获取成功",transactionRecordUserVOList);
    }

    @GetMapping("acceptTransaction/{id}")
    public AjaxResult acceptTransaction(@PathVariable("id") Long id) {

        int result = transactionRecordService.acceptTransaction(id);
        if(result==0){
            return AjaxResult.error("接受交易失败，请联系客服");
        }

        return AjaxResult.success("已接受交易");
    }
    //带数据的更新交易记录
    @PutMapping("acceptTransaction")
    public AjaxResult acceptTransaction(@RequestBody AcceptTransactionDTO acceptTransactionDTO) {


        int result = transactionRecordService.acceptTransactionByData(acceptTransactionDTO);
        if(result==0){
            return AjaxResult.error("接受交易失败，请联系客服");
        }
        return AjaxResult.success("接受交易成功");
    }

}
